from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .models import Area
from . import serializers
# Create your views here.

class AreasViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    #禁用分页后端：应为省级数据需要一次性全部拿出来，不要哦分页的获取
    pagination_class = None

    #指定查询集
    # queryset = Area.objects.all()
    def get_queryset(self):
        if self.action == 'list':
            # 查询省级数据
            return Area.objects.filter(parent=None)
        else:
            # 查询城市和区县数据
            return Area.objects.all()
    #指定序列化器
    # serializer_class = serializers.AreaSerializer

    def get_serializer_class(self):
        if self.action =='list':
            return serializers.AreaSerializer
        else:
            return serializers.SubAreaSerializer


